// pages/upload/albumList.js
const app = getApp()
const { wxPost, wxGet, formatTime, commonShare, host, getJwt } = require('../../utils/common.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    waitLoading: true,
    limit: 6,
    page: 0,
    pageObj: null,
    albumList: [],
    isGuest:true
  },
  onShow: function (option) {
    if (app.globalData.uploadImg) {
      this.loadAlbumList()
      app.globalData.uploadImg = null
    }
  },
  onLoad: function (options) {
    var pages = getCurrentPages()
    var prevPage = pages[pages.length - 2] //上一个页面
    if (prevPage) {
      this.setData({
        infoId: options.infoId || '',
        isGuest: !!prevPage.data.isGuest
      })
    } else {
      this.setData({
        infoId: options.infoId || '',
        isGuest: true
      })
    }
    this.loadAlbumList()
  },
  loadAlbumList: function () {
    const that = this
    wx.showLoading({
      title: '加载中...'
    })
    const { page, limit, infoId } = this.data

    wxGet(`/assetsInfo/albumList/${infoId}`, { start: page * limit, limit, type: 0 }, ({ data }) => {
      if (data.errorCode === 0) {
        that.setData({
          albumList: page ? that.data.albumList.concat(data.list) : data.list,
          pageObj: data.pageObj
        })
      }
    }, null, () => {
      that.setData({ waitLoading:false})
      wx.hideLoading()
    })
  },
  clickFabButton(e) {
    console.log('onClick', e.detail)
    if (e.detail.index === 0) {
      wx.navigateTo({
        url: 'albumAdd'
      })
    }
  },
  onPullDownRefresh: function () {
  },
  onReachBottom: function () {
  },
  onShareAppMessage: function () {
    return commonShare()
  },
  viewGallery: function (e) {
    const { item } = e.currentTarget.dataset
    if (item) {
      wx.navigateTo({
        url: `albumView?albumId=${item.id}&name=${item.year}年${item.month}月`
      })
    }
  },
  editConfirm: function (e) {
    const { item } = e.currentTarget.dataset

    wx.showActionSheet({
      itemList: ['修改', '取消'],
      success(res) {
        if (res.tapIndex === 0) {
          wx.navigateTo({
            url: `albumEdit?id=${item.id}&name=${item.name}&limits=${item.limits}&description=${item.description}`
          })
        } else if (res.tapIndex === 1) {

        }
      },
      fail(res) {
      }
    })
  },
  loadMoreList: function () {
    const { pageObj, page } = this.data
    if (pageObj && pageObj.haxNextPage) {
      this.setData({
        page: page + 1
      })
      this.loadAlbumList()
    }
  },
  uploadImg() {
    if (this.data.isGuest){
      wx.showToast({
        title: '当前内容为测试数据,不能上传图片哦!',
        icon:'none',
        duration:2000
      })
      return 
    }
    wx.navigateTo({
      url: `uploadImg?infoId=${this.data.infoId}`,
    })
  }
})